/* ************************************
 * GazeVis
 * 
 * ************************************
 */

static final color MOVE_COLOR = #ADFF2F;
static final int CALIBRATE_TOLERANCE = 100;

// 
// class MoveTracker
// ________________________________________________________________________________________________________
class MoveTracker
{
	//Attributes
	ArrayList fixInterest;
	int prevTime;
    boolean change;
    
    int state;
	float offset_x;
	float offset_y;
    
	//Constuctor
	MoveTracker( )
	{
		this.fixInterest = new ArrayList();
		this.prevTime = 0;
        this.change = false;
        
        state = 0;
        offset_x = VIS_WIDTH/2;
        offset_y = VIS_HEIGHT/2;        
	}
  
    void changeState( int state )
    {
        this.state = state; 

        if( state == PHASE_START || state == 0 )
        {
        }
        else if( state == PHASE_QUESTION ) 
        {
            int x , y;
            int offset = CALIBRATE_TOLERANCE;
            float mid_x = VIS_WIDTH / 2;
            float mid_y = VIS_HEIGHT / 2;
            
            for ( int index = fixInterest.size() -1 ; index >= 0 ; index--)
            {
                Fixation lastFix = (Fixation) fixInterest.get( index );
                x = lastFix.x;
                y = lastFix.y;
                
                if( x > mid_x - offset && x < mid_x + offset && 
                   y > mid_y - offset && y < mid_y + offset)
                {
                    offset_x = x;
                    offset_y = y;
                    break;
                }
                
            }
            println("Quesiton");
            println("\tOffset:  " + offset_x + " , " + offset_y);
            float diff_x = VIS_WIDTH/2 - offset_x;
            float diff_y = VIS_HEIGHT/2 - offset_y;
            println("\tDiff:    " + diff_x + " , " + diff_y);
            
        }
        else if( state == PHASE_VIS ) 
        {
        }
        
        fixInterest.clear();
        this.change = false;   

    
    }
    
	void reset()
	{     
        fixInterest.clear();
        this.change = false;   
	}
  
	//void update( Fixation target )
	//	Update the moveTracker List with a potentially new fixation.
	void update( Fixation target )
	{
		//get the prev fixation if the list is not empty.
		if( !fixInterest.isEmpty() )
		{
			int numFix = this.fixInterest.size();      
			Fixation prev = (Fixation) fixInterest.get( numFix - 1 );
                  
			//if they are the about the same fixation point
            if( target.x <= 0 && target.y <= 0 )
            {
                return;
            }
			else if( target.within( prev ) )			//Same fixation
			{
				return;
			}
			else							//~Same Fixation
			{
				prev.timeElapsed = target.timestamp - prevTime;
			}
		}
		//New fixation to be added
		prevTime = target.timestamp;
		fixInterest.add( target );
	}
	
    /////////////////////////////////////////////////////////////////////////////
	void draw()
	{
		if( !fixInterest.isEmpty() )
		{
			int numFix = this.fixInterest.size();      
		
            float colorIter = 255 / numFix;
            
            float diff_x = VIS_WIDTH/2 - offset_x;
            float diff_y = VIS_HEIGHT/2 - offset_y;
            
			for ( int curFix = 0 ; curFix < (numFix-1) ; curFix++ )
			{
				Fixation cur = (Fixation) fixInterest.get( curFix );
				Fixation next = (Fixation) fixInterest.get( curFix+1 );
			
                pushStyle();
                {
                    //stroke( MOVE_COLOR );
                    //stroke( colorIter * curFix );
                    float r = 255;
                    float g = 255 - curFix * colorIter;
                    float b = 0;
                    
                    stroke( r , g , b );
                    
                    strokeWeight( 2.5 );
			
                    line ( cur.x+diff_x , cur.y+diff_y , next.x+diff_x , next.y+diff_y );
                    ellipse( next.x+diff_x , next.y+diff_y , 25 , 25);
                }	
                popStyle();

            }

            pushStyle();
            {
                stroke( 0, 255, 0 );
                strokeWeight(2.5);
                
                float x = offset_x;
                float y = offset_y;
                
                ellipse( x , y, 5 , 5 );
            }	

            pushStyle();
            {
                stroke( 255, 0 , 0 );
                strokeWeight(2.5);
                
                float x = VIS_WIDTH / 2;
                float y = VIS_HEIGHT / 2;
                
                ellipse( x , y, 5 , 5 );
            }	


        
        }
	}
}
